/**
 * Created by shidaiapp on 2017/10/16.
 */
import actionUrls from "../../../../api/ApiUrls";
import {createDownloadForm} from "../../../js/downloadUtils"
import productData from "../list/list_data"

/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 60;
  tiyData.tPage.currentPage = 1;
  tiyData.tParam.productCode = '';
  tiyData.tParam.collectionProductCode = '';
  tiyData.tParam.gender = '';
  tiyData.tParam.choosed_classify = '';
  tiyData.tParam.classifyLevel = '';
  tiyData.tParam.choosedCategory = "",
  tiyData.tParam.categoryLevel ="",
  tiyData.tParam.is_prefer = '0';
  tiyData.tParam.products.splice(0, tiyData.tParam.products.length);
  tiyData.tData.categoryList.splice(0, tiyData.tData.categoryList.length);
  tiyData.tStatus.modalSequence.isShow = false;
  tiyData.tStatus.modalSequence.isError = false;
  tiyData.tStatus.modalSequence.errorMsg = "";
  self.productList = [];
};

/**
 * 上下架
 * @param self
 * @param tiyData
 * @param type//0:删除 1:上下架 2:置顶 7:上下架色卡 8:删除色卡
 */
export const action = (self, tiyData, type, index) => {
  let title = '';
  let content = '';
  let product;
  let productColor;
  if (2 >= type) {
    product = self.productList[index];
  } else {
    productColor = tiyData.tData.productColorList[index];
  }
  switch (type) {
    case 0:
      title = '确认删除';
      content = '是否确定将产品移入回收站（移入后可从回收站找回）';
      break;
    case 1:
      title = '确认上/下架';
      content = '确定' + (product.state == 0 ? '下架' : '上架') + '产品' + product.code;
      break;
    case 2:
      title = '确认置顶';
      tiyData.tStatus.modalAction.isTop = (product.sequence != 100);
      tiyData.tStatus.modalAction.product.sequence = tiyData.tStatus.modalAction.isTop? 100 : 10000;
      content = '确定' + (tiyData.tStatus.modalAction.isTop ? '置顶' : '取消置顶') + '产品' + product.code;
      break;
    case 7:
      title = '确认上/下架色卡';
      content = '确定' + (productColor.state == 100 ? '下架' : '上架') + '色卡' + productColor.colorName;
      if (productColor.state == 100) {
        tiyData.tStatus.modalAction.productColor.state = 200;
      } else if (productColor.state == 200) {
        tiyData.tStatus.modalAction.productColor.state = 100;
      }
      break;
    case 8:
      title = '确认删除';
      content = '移入后可从回收站找回!是否确定将产品' + productColor.colorName + '移入回收站';
      tiyData.tStatus.modalAction.productColor.state = 300;
      break;
    default:
      break;
  }
  if (2 >= type) {
    tiyData.tStatus.modalAction.product.id = product.id;
    tiyData.tStatus.modalAction.product.code = product.code;
    tiyData.tStatus.modalAction.product.state = product.state;
  } else {
    tiyData.tStatus.modalAction.productColor.productId = productColor.productId;
    tiyData.tStatus.modalAction.productColor.colorId = productColor.id;
  }
  tiyData.tStatus.modalAction.title = title;
  tiyData.tStatus.modalAction.content = content;
  tiyData.tStatus.modalAction.type = type;
  tiyData.tStatus.modalAction.isShow = true;

};
/**
 * 上下架
 * @param self
 * @param tiyData
 * @param type 3:批量删除  4:批量上下架   6:批量添加标签
 */
export const actionBatch = (self, tiyData, type) => {
  let title = '';
  let content = '';
  switch (type) {
    case 3:
      title = '确认删除';
      content = '确定批量删除';
      break;
    case 4:
      title = '确认上/下架';
      content = '确定批量' + (tiyData.tParam.is_prefer == '0' ? '下架' : '上架') + '产品';
      break;
    default:
      break;
  }
  tiyData.tStatus.modalAction.title = title;
  tiyData.tStatus.modalAction.content = content;
  tiyData.tStatus.modalAction.type = type;
  tiyData.tStatus.modalAction.isShow = true;
};

/**
 * 上下架
 * @param self
 * @param tiyData
 * @param type 0:删除 1:上下架 2:置顶 3:批量删除  4:批量上下架  6:批量添加标签
 */
export const actionDone = (self, tiyData) => {
  switch (tiyData.tStatus.modalAction.type) {
    case 0:
      webDelete(self, tiyData);
      break;
    case 1:
      webSale(self, tiyData);
      break;
    case 2:
      setSequence(self, tiyData);
      break;
    case 3:
      webBatchDelete(self, tiyData);
      break;
    case 4:
      webBatchSale(self, tiyData);
      break;
    case 6:
      console.log('批量添加标签');
      break;
    default:
      break;
  }
};

/**
 * 将饰品移入回收站
 * @param self
 * @param tiyData
 */
function webDelete(self, tiyData) {
  self.$Spin.show();
  self.$apiProduct.productToRecycle({
    "id": tiyData.tStatus.modalAction.product.id,
    "code": tiyData.tStatus.modalAction.product.code,
    "state": -2
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      webList(self, tiyData);
      self.$Message.success('提交成功!');
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
}
/**
 * 上下架某个产品
 * @param self
 * @param tiyData
 */
function webSale(self, tiyData) {
  self.$Spin.show();
  self.$apiProduct.changeProductState({
    "id": tiyData.tStatus.modalAction.product.id,
    "code": tiyData.tStatus.modalAction.product.code,
    "state": tiyData.tStatus.modalAction.product.state == 0 ? -1 : 0
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      webList(self, tiyData);
      self.$Message.success('提交成功!');
    } else {
      self.$Message.error("产品上架失败，"+response.errorMessage);
    }
  });
}

/**
 * 批量 移入回收站
 * @param self
 * @param tiyData
 */
function webBatchDelete(self, tiyData) {
  if (0 == tiyData.tParam.products.length) {
    self.$Message.error('请选择要操作的产品!');
    return;
  }
  let ids = [];
  tiyData.tParam.products.forEach(function (item, index) {
    ids.push(item.id);
  });
  self.$Spin.show();
  self.$apiProduct.batchToRecycle({
    "ids": ids
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      webList(self, tiyData);
      self.$Message.success('提交成功!');
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
}
/**
 * 批量 上架/下架
 * @param self
 * @param tiyData
 */
function webBatchSale(self, tiyData) {
  if (0 == tiyData.tParam.products.length) {
    self.$Message.error('请选择要操作的产品!');
    return;
  }
  let ids = [];
  tiyData.tParam.products.forEach(function (item, index) {
    ids.push(item.id);
  });

  self.$Spin.show();
  self.$apiProduct.batchUpdateState({
    "ids": ids,
    "state": tiyData.tParam.is_prefer == '0' ? -1 : 0// 0:上架  -1:下架
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      webList(self, tiyData);
      self.$Message.success('提交成功!');
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
}

/**
 * 请求数据
 * @param self
 * @param tiyData
 */
export const webList = (self, tiyData) => {
  self.loadingData = true;
  self.$Spin.show();
  self.param.products.splice(0,self.param.products.length);
  self.$apiProduct.getProductList({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "productCode": tiyData.tParam.productCode,
    "collectionProductCode": tiyData.tParam.collectionProductCode,
    "choosed_classify": tiyData.tParam.choosedCategory,
    "classifyLevel": tiyData.tParam.categoryLevel,
    "is_prefer": tiyData.tParam.is_prefer,
    "gender": tiyData.tParam.gender,
    "sourceState": tiyData.tParam.sourceState,
    "sortHidden": tiyData.tParam.orderBy,
  })
    .then(function (response) {
      self.$Spin.hide();
      self.loadingData = false;
      if (response.success) {
        tiyData.tParam.length = [];
        let productPage = response.result;
        tiyData.tPage.total = productPage.total;
        tiyData.tPage.stepNumber = productPage.pageSize;

        self.productList = self.$yFreeze(productPage.list);

        tiyData.tParam.squence.choosedCategory = tiyData.tParam.choosedCategory;
        tiyData.tParam.squence.choosedLevel = tiyData.tParam.categoryLevel;
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

/**
 * 选择产品后的处理
 * @param self
 * @param tiyData
 */
export const selectedChange = (self, tiyData, selection,) => {
  let ding=false;
  let down=false;
  selection.forEach(item=>{
    if(item.sequence==100){
      ding=true;
    }
    if(item.sequence!=100){
      down=true;
    }
  });
  tiyData.tStatus.isTop = down;
  tiyData.tStatus.topTitle = down ? '批量置顶' : '批量取消置顶';
  tiyData.tStatus.topHidden =(ding==down)?false:true;
  tiyData.tStatus.saleTitle = tiyData.tParam.is_prefer == '0' ? '批量下架' : '批量上架';
};

/**
 *
 * @param self
 * @param tiyData
 * @param is_prefer 是否导出下架产品
 * @param isExportAll 是否全部/按分类导出
 */
export const exportExcel=(self,tiyData, is_prefer, isExportAll)=>{
  if(is_prefer == 0 && !self.productCodes && !isExportAll){
    self.$Message.error('按编码导出产品时必须录入产品编码!');
    return;
  }
  self.$Spin.show();
  self.$apiProduct.exportProductToExcel({
    "is_prefer":is_prefer,
    "productCodes":self.productCodes,
    "choosedClassify": tiyData.tParam.choosedCategory,
    "classifyLevel": tiyData.tParam.categoryLevel,
    "isExportAll" : isExportAll
  }, 120) .then(function (response) {
    if (response.success) {
      if(response.result.errorCodes) {
        self.isShowErrorExportDialog = true;
        self.productErrorCodes = response.result.errorCodes;
      }
      let downloadForm = createDownloadForm(response.result, actionUrls.URL_DOWNLOAD_EXCEL);
      downloadForm.submit();
    } else {
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  })
};

/**
 * 批量排序操作页面
 * @param self
 * @param tiyData
 */
export const batchSequenceView =(self,tiyData)=>{
  tiyData.tStatus.modalSequence.isShow = true;
  tiyData.tStatus.modalSequence.isError = false;
  tiyData.tStatus.modalSequence.errorMsg = "";
};

/**
 * 批量排序
 * @param self
 * @param tiyData
 */
export const batchSequenceFun =(self,tiyData)=>{
    let ids = [];
    tiyData.tParam.products.forEach(function (item, index) {
      ids.push(item.id);
    });
    self.$Spin.show();
    self.$apiProduct.batchSequenceProduct({
      productIds: ids,
      squenceNumber: tiyData.tParam.squence.squenceNumber
    }).then(function (response) {
      self.$Spin.hide();
      if(tiyData.tStatus.modalSequence.isShow){
        tiyData.tStatus.modalSequence.isShow = false;
      }
      if (response.success) {
        webList(self,tiyData);
        self.$Message.success('修改排序成功!');
      } else {
        self.$Message.error(response.errorMessage);
      }
    })
};

export const showOrderList =(self,tiyData,productId)=>{
  self.$Spin.show();
  self.$apiProduct.getProductOrders({
    "productId": productId
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.orderList.splice(0,tiyData.tData.orderList.length)
      let orderList = response.result;
      orderList.forEach(
        function (item) {
          tiyData.tData.orderList.push(item);
        }
      );
      tiyData.tStatus.modalOrder.isShow = true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 *
 * @param self
 * @param tiyData
 * @param is_prefer 是否导出下架产品
 * @param isExportAll 是否全部/按分类导出
 */
export const exportExcelForExhibit=(self,tiyData)=>{
  self.$Spin.show();
  self.$apiProduct.exportExcelForExhibit({
    "choosedClassify": tiyData.tParam.choosedCategory,
    "classifyLevel": tiyData.tParam.categoryLevel,
  }, 120) .then(function (response) {
    if (response.success) {
      if(response.result.errorCodes) {
        self.isShowErrorExportDialog = true;
        self.productErrorCodes = response.result.errorCodes;
      }
      let downloadForm = createDownloadForm(response.result, actionUrls.URL_DOWNLOAD_EXCEL);
      downloadForm.submit();
    } else {
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  })
};

/**
 * 设置产品排序
 * @param self
 * @param tiyData
 */
function setSequence(self, tiyData) {
  self.$Spin.show();
  self.$apiProduct.productTop({
    "id": tiyData.tStatus.modalAction.product.id,
    "code": tiyData.tStatus.modalAction.product.code,
    "sequence": tiyData.tStatus.modalAction.product.sequence
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      webList(self, tiyData);
      self.$Message.success('提交成功!');
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
}

export const getSourceState = (state) => {
  let element = productData.tData.sourceStates.find(element => element.value == state);
  return element?element.label:"";
}
